const router = require('koa-router')()
const passport = require('./passport')

router.post('/login', ctx => {
  // 会调用策略
  return passport.authenticate('local',
    function(err, user, info, status) {
      ctx.body = {user, err, info, status}
      return ctx.login({id: 1, username: 'admin', password: '123456'})
    })(ctx)
})

router.get('/logout', ctx => {
  ctx.logout()
  ctx.body = {auth: ctx.isAuthenticated(), user: ctx.state.user}
})

router.get('/api/test', (ctx, next) => {
  if(ctx.isAuthenticated()) {
    ctx.body = {
      message: '已授权'
    }
  } else {
   ctx.status = 401
   ctx.body = {
    message: '未授权'
   }
 }
})

module.exports = router